#!/bin/bash

# AI Video Hub 启动脚本

echo "🚀 启动 AI Video Hub..."
echo

# 检查Python是否安装
if ! command -v python &>/dev/null; then
    echo "❌ Python 未找到，请先安装 Python"
    exit 1
fi

# 检查是否已存在虚拟环境
if [ ! -d ".venv" ]; then
    echo "📦 创建虚拟环境..."
    python -m venv .venv
fi

# 激活虚拟环境
echo "🔌 激活虚拟环境..."
source .venv/bin/activate

# 检查并安装依赖
if [ ! -f "requirements.txt" ] || [ ! -f ".requirements_installed" ] || [ requirements.txt -nt .requirements_installed ]; then
    echo "📥 安装项目依赖..."
    pip install --upgrade pip
    pip install -r requirements.txt
    touch .requirements_installed
else
    echo "✅ 依赖已安装，跳过安装步骤"
fi

# 检查环境变量文件
if [ ! -f ".env" ]; then
    echo "⚠️  警告: .env 文件不存在，创建示例文件..."
    echo "# AI Video Hub 环境变量配置" > .env
    echo "DATABASE_URL=sqlite:///./aivideo.db" >> .env
    echo "REDIS_URL=redis://localhost:6379/0" >> .env
    echo "OPENAI_API_KEY=" >> .env
    echo "ANTHROPIC_API_KEY=" >> .env
    echo "OLLAMA_BASE_URL=http://localhost:11434" >> .env
    echo "VIDEO_OUTPUT_DIR=videos" >> .env
    echo "MAX_VIDEO_DURATION=300" >> .env
    echo "MAX_FILE_SIZE=50000" >> .env
    echo "已创建 .env 示例文件，请根据需要填写配置"
fi

# 创建必要的目录
echo "📁 创建项目目录..."
mkdir -p videos
mkdir -p logs

echo
echo "🎬 启动 AI Video Hub 服务..."
echo "🌐 访问 http://127.0.0.1:8000 查看服务"
echo "📋 API 文档: http://127.0.0.1:8000/docs"
echo

# 启动服务
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
